#include "Point3F.h"

#include <math.h>
#include <stdio.h>

CPoint3F::CPoint3F(void)
{
	m_x = 0.0f;
	m_y = 0.0f;
	m_z = 0.0f;
}


CPoint3F::~CPoint3F(void)
{
	m_x = 0.0f;
	m_y = 0.0f;
	m_z = 0.0f;
}

CPoint3F::CPoint3F(const float x, const float y, const float z)
{
	m_x = x;
	m_y = y;
	m_z = z;
}

float InnerProduct(const CVector3F &a, const CVector3F &b)
{
	float res = a.m_x * b.m_x + a.m_y * b.m_y + a.m_z * b.m_z;
	if (res > 1.0f)
		res = 1.0f;
	return res;
}

float InnerProduct(const CVector3F *a, const CVector3F *b)
{
	float res = a->m_x * b->m_x + a->m_y * b->m_y + a->m_z * b->m_z;
	if (res > 1.0f)
		res = 1.0f;
	return res;
}
float Distance(const CPoint3F &a, const CPoint3F &b)
{
	CPoint3F c;
	c.m_x = a.m_x - b.m_x;
	c.m_y = a.m_y - b.m_y;
	c.m_z = a.m_z - b.m_z;

	float dis = sqrtf(c.m_x * c.m_x + c.m_y * c.m_y + c.m_z * c.m_z);
	return dis;
}

float Distance(const CPoint3F *a, const CPoint3F *b)
{
	float x = a->m_x - b->m_x;
	float y = a->m_y - b->m_y;
	float z = a->m_z - b->m_z;
	float d = sqrtf(x * x + y * y + z * z);
	return d;
}

void Normalize(CVector3F &v)
{
	float l = sqrtf(v.m_x * v.m_x + v.m_y * v.m_y + v.m_z * v.m_z);
	if (l > 0.0f) {
		v.m_x = v.m_x / l;
		v.m_y = v.m_y / l;
		v.m_z = v.m_z / l;
	} else {
		v.m_x = 0.0f;
		v.m_y = 0.0f;
		v.m_z = 0.0f;
	}
}
void Normalize(CVector3F *v)
{
	float l = sqrtf(v->m_x * v->m_x + v->m_y * v->m_y + v->m_z * v->m_z);
	if (l > 0.0f) {
		v->m_x = v->m_x / l;
		v->m_y = v->m_y / l;
		v->m_z = v->m_z / l;
	} else {
		v->m_x = 0.0f;
		v->m_y = 0.0f;
		v->m_z = 0.0f;
	}
}

CPoint3F CPoint3F::operator+(const CPoint3F &right)
{
	CPoint3F res;
	res.m_x = this->m_x + right.m_x;
	res.m_y = this->m_y + right.m_y;
	res.m_z = this->m_z + right.m_z;
	return res;
}

CPoint3F CPoint3F::operator-(const CPoint3F &right)
{
    //printf("operator - : (%f %f %f) - (%f %f %f)\n", 
    //        m_x, m_y, m_z, right.m_x, right.m_y, right.m_z);
	CPoint3F res;
	res.m_x = this->m_x - right.m_x;
	res.m_y = this->m_y - right.m_y;
	res.m_z = this->m_z - right.m_z;
	return res;
}

CPoint3F CPoint3F::operator*(const float &left)
{
	CPoint3F res;
	res.m_x = this->m_x * left;
	res.m_y = this->m_y * left;
	res.m_z = this->m_z * left;
	return res;
}

CPoint3F CPoint3F::operator/(const float &left)
{
	CPoint3F res;
	res.m_x = this->m_x / left;
	res.m_y = this->m_y / left;
	res.m_z = this->m_z / left;
	return res;
}

void CPoint3F::Normalize()
{
	float len = sqrtf(m_x * m_x + m_y * m_y + m_z * m_z);
	if (len > 0.0f) {
		m_x /= len;
		m_y /= len;
		m_z /= len;
	}
}

float CPoint3F::Length() const
{
	float len = sqrtf(m_x * m_x + m_y * m_y + m_z * m_z);
	return len;
}
